package teacher;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.graphics.PixelFormat;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.project.institute.DAO.InstituteFragmentActivityDAO;
import com.project.trac.R;

import core.modules.DatabaseConnector;


public class NewThread extends InstituteFragmentActivityDAO implements OnClickListener {
	
	private Spinner sp_mySection;
	
	private EditText et_title;
	
	private EditText et_activity_details;
	
	private Button btn_create;
	private Button btn_clear;
	
	boolean submitted;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.teacher_new_thread);
		setTitle("New Topic - " + getSubjectName());
		
		btn_create = (Button) findViewById(R.id.btn_create);
		btn_clear = (Button) findViewById(R.id.btn_clear);
		
		btn_create.setOnClickListener(this);
		btn_clear.setOnClickListener(this);
		
		et_title = (EditText) findViewById(R.id.et_title);
		et_activity_details = (EditText) findViewById(R.id.et_activity_details);
		
		sp_mySection = (Spinner) findViewById(R.id.sp_mySection);
		
		new AsynchronousTask().execute("initialize");
	}
	
	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
			case R.id.btn_create:
				
				if (noEmptyFields()) {
					new AsynchronousTask().execute("submit");
				}
				
				break;
			case R.id.btn_clear:
				et_activity_details.setText("");
				break;
		}
		
	}
	
	@Override
	public void onAttachedToWindow() {
		super.onAttachedToWindow();
		Window window = getWindow();
		window.setFormat(PixelFormat.RGBA_8888);
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}
	
	private boolean noEmptyFields() {

		if (et_title.getText().toString().length() == 0) {
			et_title.setError("required");
			
			return false;
		}
		
		if (et_activity_details.getText().toString().length() == 0) {
			et_activity_details.setError("required");
			
			return false;
		}
		
		return true;

	}

	private class AsynchronousTask extends AsyncTask<String, String, String> {
		
		HashMap<String, String> m;
		ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();
		List<String> list_section = new ArrayList<String>();
		ArrayAdapter<String> arrayAdapter_section;

		protected void onPreExecute() {
			super.onPreExecute();
		}

		protected String doInBackground(String... args) {
			try {
				
				if (args[0].equalsIgnoreCase("initialize")) {
					ResultSet rs = DatabaseConnector.getStatement().executeQuery(
							" SELECT schedule.section FROM schedule"
							+ " INNER JOIN curriculum ON curriculum.id = schedule.cid"
							+ " WHERE uid = " + getUserID() + " AND cid = " + getSubjectID());

					while (rs.next()) {
						
						m = new HashMap<String, String>();
						m.put("section", rs.getString("section"));
						
						list_section.add(rs.getString("section"));
						
						arrayList.add(m);
					}
				}
				
				if (args[0].equalsIgnoreCase("submit")) {
					
					DatabaseConnector.setPreparedStatement(
							"INSERT INTO forum (uid, cid, sid, section, title, content, isThread) VALUES (?,?,?,?,?,?,?)");
					PreparedStatement pps = DatabaseConnector.getPreparedStatement();
					pps.setInt(1, getUserID());
					pps.setInt(2, getSubjectID());
					pps.setInt(3, getScheduleID());
					pps.setString(4, sp_mySection.getSelectedItem().toString());
					pps.setString(5, et_title.getText().toString());
					pps.setString(6, et_activity_details.getText().toString());
					pps.setInt(7, 2);

					submitted = true;
					pps.executeUpdate();
					pps.close();
				}

			} catch (SQLException e) {
				DatabaseConnector.Logger(getClass(), "doInBackground", e);
			} 

			return null;
		}

		protected void onPostExecute(String postExecute) {
			
			runOnUiThread(new Runnable() {

				public void run() {
						
					arrayAdapter_section = new ArrayAdapter<String>(NewThread.this, android.R.layout.simple_spinner_item, list_section);
						arrayAdapter_section.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
						
					sp_mySection = (Spinner) findViewById(R.id.sp_mySection);
						sp_mySection.setAdapter(arrayAdapter_section);
				}
			});
			
			if (submitted) {
				Toast.makeText(getApplicationContext(), "thread started", Toast.LENGTH_SHORT).show();
				NewThread.this.finish();
			}			
		}
		
	}
}